package common

import (
	"encoding/json"
	redis2 "take-a-break/app/models/redis"
	"take-a-break/component/redis"
	"take-a-break/exception"
	"take-a-break/utils/common"
)

// Repeat 请求频繁
func Repeat(method string, data interface{}, expTime int) (err error) {
	dataStr, _ := json.Marshal(data)
	str := method + string(dataStr)
	md5Str := common.Md5Encode(str)

	//限制请求
	times, _ := redis.Redis().TTL(redis2.GetRepeatKey(md5Str))
	if times > 0 {
		err = exception.DoNotRepeat
		return
	}
	redis.Redis().SetString(redis2.GetRepeatKey(md5Str), str, expTime)
	return
}
